<template>
	<view>
		<view class="user_top">
			<view class="left">
				<u--image :src="avatar" width="140rpx" height="140rpx" shape="circle" mode="widthFix">
					<view slot="error" style="font-size: 24rpx;">加载失败</view>
				</u--image>
				<view class="text" @click="showForm">{{username || '点击登录'}}</view>
			</view>
			<u-icon :name="iconName" color="#fff" @click="onClickIcon"></u-icon>
		</view>

		<u-popup :show="formVisible" mode="center" @close="closeForm" :customStyle="{padding:'30rpx'}">
			<u--form labelPosition="left" :model="formModel" :rules="rules" ref="form" labelWidth="100">
				<u-form-item label="用户名" prop="userInfo.name" borderBottom>
					<u--input v-model.trim="formModel.userInfo.name" border="none" clearable placeholder="请填写用户名">
					</u--input>
				</u-form-item>
				<u-form-item label="密码" prop="userInfo.password" borderBottom>
					<u--input type="password" v-model.trim="formModel.userInfo.password" border="none" clearable
						placeholder="请填写密码">
					</u--input>
				</u-form-item>
			</u--form>
			<u-button type="primary" @click="submit">提交</u-button>
		</u-popup>

		<!-- （注意：关闭事件需要自行处理，只会在开启closeOnClickOverlay后点击遮罩层执行close回调） -->
		<u-modal :show="modalVisible" title="提示" content='是否确定退出登陆' :closeOnClickOverlay="true" showCancelButton
			@cancel="closeModal" @close="closeModal" @confirm="handleLogOut">
		</u-modal>
	</view>
</template>

<script>
	import request from '../../request/request.js'
	import {
		apiAuthLoginByWeb
	} from '../../request/api.js'
	const defaultAvatar =
		''


	export default {
		data() {
			return {
				avatar: defaultAvatar,
				username: '',
				iconName: 'arrow-right',
				formVisible: false,
				formModel: {
					userInfo: {
						name: '',
						password: ''
					}
				},
				rules: {
					'userInfo.name': {
						type: 'string',
						required: true,
						message: '请填写用户名',
						trigger: ['blur', 'change']
					},
					'userInfo.password': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					}
				},
				modalVisible: false
			};
		},
		onLoad() {
			const token = uni.getStorageSync('token')
			const userInfo = uni.getStorageSync('userInfo')
			if (token && userInfo) {
				const {
					avatar,
					username
				} = userInfo
				this.avatar = avatar
				this.username = username
				this.iconName = 'close'
			}
		},
		methods: {
			showForm() {
				const token = uni.getStorageSync('token')
				if (!token) {
					this.formVisible = true
				}
			},
			closeForm() {
				this.formVisible = false
			},
			onClickIcon() {
				const token = uni.getStorageSync('token')
				if (token) {
					// 二次确认 退出登录
					this.modalVisible = true
				} else {
					// 弹窗登录
					this.formVisible = true
				}
			},
			closeModal() {
				this.modalVisible = false
			},
			submit() {
				this.$refs.form.validate().then(() => {
					const {
						name,
						password
					} = this.formModel.userInfo
					request({
						url: apiAuthLoginByWeb,
						method: 'POST',
						data: {
							username: name,
							pwd: password
						},
						successCb: res => {
							console.log(res)
							const {
								token,
								userInfo
							} = res
							const {
								avatar,
								username
							} = userInfo
							uni.setStorageSync('token', token)
							uni.setStorageSync('userInfo', userInfo)
							this.avatar = avatar
							this.username = username
							this.iconName = 'close'
							this.formModel.userInfo.name = ''
							this.formModel.userInfo.password = ''
							this.formVisible = false
						}
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			handleLogOut() {
				uni.clearStorageSync()
				this.avatar = defaultAvatar
				this.username = ''
				this.iconName = 'arrow-right'
				this.closeModal()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user_top {
		background: #333;
		color: #fff;
		padding: 40rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.left {
			display: flex;
			align-items: center;

			.text {
				font-size: 26rpx;
				line-height: 3;
				margin-left: 20rpx;
			}
		}
	}

	/deep/ .u-form {
		margin-bottom: 50rpx;
	}

	/deep/.u-modal__content__text {
		text-align: center;
	}
</style>
